<?php

class ControllerModuleWebboard extends Controller {
	private $error = array(); 
	
	public function index() {   
		$this->load->language('module/webboard');

		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('setting/setting');
				
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {

			$this->model_setting_setting->editSetting('webboard', $this->request->post);		
					
			$this->session->data['success'] = $this->language->get('text_success');
						
			$this->redirect((HTTPS_SERVER . 'index.php?token=' . $this->session->data['token'] . '&route=extension/module'));
		}
				
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');

		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_num_show'] = $this->language->get('entry_num_show');
		$this->data['entry_num_read'] = $this->language->get('entry_num_read');
		$this->data['entry_guest'] = $this->language->get('entry_guest');
		$this->data['entry_credit'] = $this->language->get('entry_credit');
		$this->data['entry_webboard'] = $this->language->get('entry_webboard');
		$this->data['entry_author'] = $this->language->get('entry_author');
		$this->data['entry_allowed'] = $this->language->get('entry_allowed');
		$this->data['entry_not_allowed'] = $this->language->get('entry_not_allowed');
		$this->data['entry_day'] = $this->language->get('entry_day');
		$this->data['text_day'] = $this->language->get('text_day');
		$this->data['text_show_limit'] = $this->language->get('text_show_limit');
		$this->data['text_left'] = $this->language->get('text_left');
		$this->data['text_right'] = $this->language->get('text_right');
		$this->data['text_home'] = $this->language->get('text_home');		
		$this->data['entry_position'] = $this->language->get('entry_position');	
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');

		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
	  		
		$this->data['positions'] = array();
		
		$this->data['positions'][] = array(
			'position' => 'left',
			'title'    => $this->language->get('text_left'),
		);
		
		$this->data['positions'][] = array(
			'position' => 'right',
			'title'    => $this->language->get('text_right'),
		);
		
		$this->data['positions'][] = array(
			'position' => 'home',
			'title'    => $this->language->get('text_home'),
		);
		
		if (isset($this->request->post['webboard_position'])) {
			$this->data['webboard_position'] = $this->request->post['webboard_position'];
		} else {
			$this->data['webboard_position'] = $this->config->get('webboard_position');
		}
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
 		if (isset($this->error['error_name'])) {
			$this->data['error_name'] = $this->error['error_name'];
		} else {
			$this->data['error_name'] = '';
		}
 		if (isset($this->error['error_num_show'])) {
			$this->data['error_num_show'] = $this->error['error_num_show'];
		} else {
			$this->data['error_num_show'] = '';
		}
 		if (isset($this->error['error_num_read'])) {
			$this->data['error_num_read'] = $this->error['error_num_read'];
		} else {
			$this->data['error_num_read'] = '';
		}
		if (isset($this->error['error_guest'])) {
			$this->data['error_guest'] = $this->error['error_guest'];
		} else {
			$this->data['error_guest'] = '';
		}
		if (isset($this->error['error_day'])) {
			$this->data['error_day'] = $this->error['error_day'];
		} else {
			$this->data['error_day'] = '';
		}
		if (isset($this->error['error_show_limit'])) {
			$this->data['error_show_limit'] = $this->error['error_show_limit'];
		} else {
			$this->data['error_show_limit'] = '';
		}


		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => (HTTPS_SERVER . 'index.php?token=' . $this->session->data['token'] . '&route=common/home'),
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => (HTTPS_SERVER . 'index.php?token=' . $this->session->data['token'] . '&route=extension/module'),
       		'text'      => $this->language->get('text_module'),
      		'separator' => ' :: '
   		);
		
   		$this->document->breadcrumbs[] = array(
       		'href'      => (HTTPS_SERVER . 'index.php?token=' . $this->session->data['token'] . '&route=module/webboard'),
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = (HTTPS_SERVER . 'index.php?token=' . $this->session->data['token'] . '&route=module/webboard');
		
		$this->data['cancel'] = (HTTPS_SERVER . 'index.php?token=' . $this->session->data['token'] . '&route=extension/module');

		if (isset($this->request->post['webboard_position'])) {
			$this->data['webboard_position'] = $this->request->post['webboard_position'];
		} else {
			$this->data['webboard_position'] = $this->config->get('webboard_position');
		}

		if (isset($this->request->post['webboard_name'])) {
			$this->data['webboard_name'] = $this->request->post['webboard_name'];
		} else {
			$this->data['webboard_name'] = $this->config->get('webboard_name');
		}
		
		if (isset($this->request->post['webboard_num_show'])) {
			$this->data['webboard_num_show'] = $this->request->post['webboard_num_show'];
		} else {
			$this->data['webboard_num_show'] = $this->config->get('webboard_num_show');
		}
		
		if (isset($this->request->post['webboard_num_read'])) {
			$this->data['webboard_num_read'] = $this->request->post['webboard_num_read'];
		} else {
			$this->data['webboard_num_read'] = $this->config->get('webboard_num_read');
		}				
		if (isset($this->request->post['webboard_guest'])) {
			$this->data['webboard_guest'] = $this->request->post['webboard_guest'];
		} else {
			$this->data['webboard_guest'] = $this->config->get('webboard_guest');
		}				
		if (isset($this->request->post['webboard_status'])) {
			$this->data['webboard_status'] = $this->request->post['webboard_status'];
		} else {
			$this->data['webboard_status'] = $this->config->get('webboard_status');
		}
		if (isset($this->request->post['webboard_day'])) {
			$this->data['webboard_day'] = $this->request->post['webboard_day'];
		} else {
			$this->data['webboard_day'] = $this->config->get('webboard_day');
		}
		if (isset($this->request->post['webboard_show_limit'])) {
			$this->data['webboard_show_limit'] = $this->request->post['webboard_show_limit'];
		} else {
			$this->data['webboard_show_limit'] = $this->config->get('webboard_show_limit');
		}
		
		if (isset($this->request->post['webboard_sort_order'])) {
			$this->data['webboard_sort_order'] = $this->request->post['webboard_sort_order'];
		} else {
			$this->data['webboard_sort_order'] = $this->config->get('webboard_sort_order');
		}				
		
		$this->template = 'module/webboard.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/webboard')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->request->post['webboard_name']) {
			$this->error['error_name'] = $this->language->get('error_name');
		}		
		if (!$this->request->post['webboard_num_show']) {
			$this->error['error_num_show'] = $this->language->get('error_num_show');
		}		
		if (!$this->request->post['webboard_num_read']) {
			$this->error['error_num_read'] = $this->language->get('error_num_read');
		}		
		if (!$this->request->post['webboard_guest']) {
			$this->error['error_guest'] = $this->language->get('error_guest');
		}		
		if (!$this->request->post['webboard_day']) {
			$this->error['error_day'] = $this->language->get('error_day');
		}		
		if (!$this->request->post['webboard_show_limit']) {
			$this->error['error_show_limit'] = $this->language->get('error_show_limit');
		}		

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}	
	}
}
?>